<?php
/**
 * Created by PhpStorm.
 * @author: 汤文辉
 * @since: 2017/6/13 12:16
 * 图书类，进行图书的各项操作:
 * array getBookInfo($book_id)      根据编号获取图书的详细信息
 * array getComment($book_id)       根据编号获取该图书的所有评论信息
 * array getComInfo($comment_id)    根据评论编号获取该评论的详细信息
 * String getUserName($user_id)     根据用户编号查找发表对应评论的用户名
 * array SearchBook($keyword)       根据关键词查找符合的图书
 *
 */
class Book{

    function __construct($con)
    {
         $this->con = $con;
    }

    /**
     * 查找数据库中图书的详细信息，包含图书的出版信息和评论信息
     * @param $book_id
     * @return array $book
     */
    function getBookInfo($book_id)
    {
        if ($this->con && $book_id) {
            //根据book_id，搜索图书具体信息
            $sql1 = "select book_name,book_price,book_author,book_isbn,book_intro,book_time from book where book_id='$book_id';";
            $result1 = mysqli_query($this->con, $sql1);
            if ($result1) {
                $b = $result1->fetch_row();
                $book['id'] = $book_id;
                $book['name'] = $b[0];
                $book['price'] = $b[1];
                $book['author'] = $b[2];
                $book['isbn'] = $b[3];
                $book['intro'] = $b[4];
                $book['time'] = $b[5];
                $book['picture'] = "book_pic/".$book_id.".jpg";
            }
        }
        if($book)
            return $book;
    }

    /**
     * 通过图书编号获得图书评论信息
     * @param $book_id
     * @return array $comment
     */
    function getComment($book_id)
    {
        $comment = array();  //comment默认为空

        if ($this->con && $book_id) {
            //查找图书评论和发表评论的用户
            $sql2 = "select comment_id,user_id from book_user_comment where book_id='$book_id';";
            $result2 = mysqli_query($this->con, $sql2);

            if ($result2) {
                while ($temp = $result2->fetch_row()) {
                    $comment["$temp[0]"]['user_id'] = $temp[1];
                    $comment["$temp[0]"]['user_name'] = $this->getUserName($temp[1]);
                    $com = $this->getComInfo($temp[0]);
                    $comment["$temp[0]"]['time'] = $com['time'];
                    $comment["$temp[0]"]['content'] = $com['content'];
                }
            }
        }
        return $comment;
    }
    /**
     * 查找评论的时间和内容，保存在数组中返回
     * @param  $comment_id
     * @return array $cominfo
     */
    function getComInfo($comment_id){
        $sql = "select comment_time,comment_content from comment where comment_id = '$comment_id';";
        $result = mysqli_query($this->con,$sql);
        $comment = $result->fetch_row();
        $cominfo['time'] = $comment[0];
        $cominfo['content'] = $comment[1];
        return $cominfo;
    }

    /**
     * 查找发表对应评论的用户名，并返回结果
     * @param $user_id
     * @return string $user[0]
     */
    function getUserName($user_id){
        $sql = "select user_name from user where user_id = '$user_id';";
        $result = mysqli_query($this->con,$sql);
        $user = $result->fetch_row();
        return $user[0];
    }

    /**
     * 根据用户输入的关键词查找符合条件的书籍
     * @param string $keyword
     * @return array $book
     */
    function SearchBook($keyword){
        $book = array();  //book默认为空

        if($this->con && $keyword){
            $sql = "select book_id,book_name,book_price,book_intro from book where book_name like '%$keyword%' union 
                select book_id,book_name,book_price,book_intro from book where book_author like '%$keyword%';";
            $result = mysqli_query($this->con,$sql);

            if($result){
                $i = 0;
                while($b=$result->fetch_row()){
                    $book[$i]['id'] = $b[0];
                    $book[$i]['name'] = $b[1];
                    $book[$i]['price'] = $b[2];
                    $book[$i]['intro'] = $b[3];
                    $book[$i]['picture'] = "book_pic/".$book[$i]['id'].".jpg";
                    ++$i;
                }
                $result->close();
            }
        }
        return $book;
    }
    /**
     * 分类显示图书
     * @param string $kind
     * @return array $book
     */
    function getBookByKind($kind){
        if($this->con && $kind){
            $sql = "select book_id, book_name,book_price,book_intro from book where book_kind='$kind';";
            if ($result = mysqli_query($this->con, $sql)) {
                $i = 0;
                while($b=$result->fetch_row()){
                    $book[$i]['id'] = $b[0];
                    $book[$i]['name'] = $b[1];
                    $book[$i]['price'] = $b[2];
                    $book[$i]['intro'] = $b[3];
                    $book[$i]['picture'] = "book_pic/".$book[$i]['id'].".jpg";
                    ++$i;
                }
                $result->close();
            }
        }
        return $book;
    }
    /**
     * 统计各类别图书的数量
     * @param $kind
     * @return array $count
     */
    function getKindNum($kind)
    {
        if ($this->con && $kind){
            for($i=0;$i<5;++$i){
                $sql = "select count(*) from book where book_kind='$kind[$i]';";
                if ($result=mysqli_query($this->con,$sql)){
                    $c=$result->fetch_row();
                    $count["$kind[$i]"] = $c[0];
                }
            }
        }
        return $count;
    }

    /**
     * 获取书籍的总数
     * @return int $count
     */
    function getBookNum(){
        if ($this->con){
            $sql = "select count(*) from book";
            if ($result=mysqli_query($this->con,$sql)){
                $c=$result->fetch_row();
                $count = $c[0];
            }
        }
        return $count;
    }
    /**
     * 随机生成图书编号
     * @return array $b_id
     */
    function RandId(){
        $i=0;
        $count = 15;
        while ($i<$count) {
            $b_id[$i] = rand(1001,1015);
            $b_id=array_unique($b_id);
            if($i==15&&($k=count($b_id)<15)){
                $count=$count+15-$k;
            }
            ++$i;
        }
        return $b_id;
    }

    function __destruct()
    {
        // TODO: Implement __destruct() method.

    }
}
